var config = require('../../../config.js')
var http = require('../../../utils/httpHelper.js')
Page({
  data: {
    userInfo:{},
    isLogin:false,
    IndexList:[],
    shopId:0,
    shop:{},
    indicatorDots:false,//是否显示面板指示点
    autoplay:false,//是否自动切换
    current:0,//当前所在index
    interval:0,//自动切换时间
    duration:200,//滑动时间
    timer:{},
    productPage:{
      current:1,
      pageSize:12,
			total:0
    }
  },
  onLoad:function(options){
    var that = this;
    let userInfo = wx.getStorageSync('user');
    if (userInfo!=null&&userInfo!=undefined&&userInfo!={}&&userInfo!='') {
      wx.showShareMenu({
        withShareTicket:true,
        menus:['shareAppMessage','shareTimeline']
      })
      that.setData({
        isLogin:true,
        userInfo:userInfo
      })
    }else {
      wx.hideShareMenu();
      wx.setStorageSync('inviteId', options.inviteId);
    }
    wx.setNavigationBarTitle({
      title: options.shopName+'-商品展示'
    })
    var shopId = options.id;
    that.getShopInfo(shopId,function(shop){
      that.setData({shop:shop});      
    })
    //获取商品列表
    that.getGoodsList(shopId,function(list){
      that.setData({
        shopId:shopId,
        IndexList:list
      });
    })
  },
  onShow:function(){
    let userInfo = wx.getStorageSync('user');
    if (userInfo!=null&&userInfo!=undefined&&userInfo!={}&&userInfo!='') {
      this.setData({
        isLogin:true,
        userInfo:userInfo
      })
    }
    if (!this.data.isLogin) {
      wx.hideShareMenu()
    }
  },
  onReachBottom(){
    if(this.timer != null){
      clearTimeout(this.timer)
    }
    this.timer = setTimeout(()=>{
      this.getMoreData();
    },1000)
  },
  getMoreData() {
    var that = this;
    that.data.productPage.current++;
    that.getGoodsList(that.data.shopId,function(list){
      let yList = that.data.IndexList;
      yList = yList.concat(list);
      that.setData({IndexList:yList});
    })
  },
  getGoodsList:function(shopId,callback){
    var that = this;
    var data = {
      appid: config.APPID,
      pageNo:that.data.productPage.current,
      pageSize:that.data.productPage.pageSize,
      shopId:shopId
    }    
    http.httpPost(config.clubApi.getProductList,data,function(res){
      if(res.code == '200'){
        var list = res.data.pageData;
        typeof callback == "function" && callback(list)
      }
    });
  },
  todetail:function(e){
    var id = e.currentTarget.id;
    wx.navigateTo({
      url: '../../detail/index?id='+id
    })
  },
  getShopInfo:function(deptId,callback){
    var data = {deptId:deptId}
    http.httpGet(config.clubApi.getShopInfo,data,function(res){
      if(res.code == '200'){
        var shop = res.data;
        typeof callback == "function" && callback(shop)
      }
    });
  },
  // 分享到朋友圈
  onShareTimeline() {
    var shop = this.data.shop;
    var url = '/pages/shop/detail/index?id='+shop.deptId+'&inviteId='+this.data.userInfo.userId+'&shopName='+shop.deptName;
    return {
      title: shop.deptName,
      path: url,
      imageUrl: shop.shopLogo,
      desc: shop.introduce
    }
  },
  // 转发
  onShareAppMessage() {
    var shop = this.data.shop;
    var url = '/pages/shop/detail/index?id='+shop.deptId+'&inviteId='+this.data.userInfo.userId+'&shopName='+shop.deptName;
    return {
      title: shop.deptName,
      path: url,
      imageUrl: shop.shopLogo,
      desc: shop.introduce
    }
  }
})